import os
import json


class ResultsWriter:
    def __init__(self):
        self._results_buffer = dict()

    def write_to_buffer(self, results, cur_day):
        self._results_buffer[str(cur_day)] = results

    def write_to_file(self, output_path="./output"):
        path = output_path + "/results.json"
        content = json.dumps(self._results_buffer, indent=4)
        try:
            if not os.path.exists(output_path):
                os.makedirs(output_path)
            with open(path, "w", encoding='utf-8') as f:
                f.write(content)
        except IOError as e:
            raise e

    def get_buffer(self):
        return self._results_buffer
